package cate.game.role.friend;

import cate.common.table.d.GDFriend;
import cate.common.util.GameResult;
import cate.game.res.MixRes;
import cate.game.role.Role;
import cate.game.role.RoleDependent;
import cate.game.role.RoleSnapshot;
import cate.game.role.friend.msg.FriendGiftDataResp;
import cate.game.role.friend.msg.FriendGiftHandleResp;
import cate.game.role.friend.msg.FriendGiftRecvResp;
import cate.game.role.friend.msg.FriendGiftSendResp;
import cate.game.videohall.vo.RoleSnapShotSimpleVO;
import com.fasterxml.jackson.annotation.JsonIgnore;
import easy.java.dev.note.NoteClass;
import easy.java.dev.note.NoteField;
import org.apache.commons.lang3.StringUtils;

import java.util.*;

@NoteClass(value = "好友礼物的列表")
public class FriendGift extends RoleDependent {
    @NoteField(value = "礼物列表",detail = "Map<好友uid, 礼物信息>")
    public Map<String, FriendGiftItem> items;

    @NoteField(value = "今日已送礼物数")
    public int sentToday;

    @NoteField(
            value = "已送过的好友的唯一ID记录",
            detail = "Map<好友uid, 是否已送过>")
    public Map<String, Boolean> sentMap;

    @NoteField(value = "今日已收礼物数")
    public int recvToday;

    @NoteField(value = "送礼总次数")
    public int sendTotal;

    @Override
    public void initialize(Role role) {
        super.initialize(role);
        if (items == null) {
            items = new HashMap<>();
        }
        if (sentMap == null) {
            sentMap = new HashMap<>();
        }
    }

    private static final long expiredTime = 3600 * 24 * 1000 * 7L;

    @Override
    public void onDaySpan(boolean silence) {
        sentToday = 0;
        recvToday = 0;
        sentMap.clear();
        items.entrySet().removeIf(e -> e.getValue() == null
                || e.getValue().recv
                || (System.currentTimeMillis() - e.getValue().sendTime) > expiredTime);
        if(!silence){
            role.sendNow(new FriendGiftDataResp(role));
        }
    }

    /**
     * 该好友今天是否已经送过礼物
     * @param friendIdHex 好友的唯一id
     * @return 是否送过礼物
     */
    public boolean getSentToday(String friendIdHex) {
        Boolean value = sentMap.get(friendIdHex);
        return value != null && value;
    }

    /**
     * 设置今天已送过该好友礼物了
     * @param friendIdHex 好友唯一id
     */
    public void setSentToday(String friendIdHex) {
        sentMap.put(friendIdHex, true);
    }

    /**
     * 收到好友的礼物
     * @param item 礼物
     */
    public void recv(FriendGiftItem item, boolean notice) {
        items.put(item.senderUid, item);
        if(notice && role.getStatus().isOnline()){
            FriendGiftRecvResp resp = new FriendGiftRecvResp();
            resp.item = item;
            role.sendNow(resp);
        }
    }

    /**
     * 好友送礼
     * @param targets 要赠送的好友数据
     */
    public MixRes giftFor(List<Friend> targets){
        sentToday += targets.size();
        sendTotal += targets.size();
        // 作为送礼人的自身可以获得的奖励
        for (Friend f : targets) {
            FriendGiftItem giftItem = new FriendGiftItem();
            giftItem.targetIdHex = f.it.uid;
            giftItem.senderUid = role.getUid();
            giftItem.sendTime = System.currentTimeMillis();
            giftItem.rs = new RoleSnapShotSimpleVO();
            giftItem.rs.read(new RoleSnapshot(role));
            //
            role.getGame().friend.recvGift(giftItem);
            setSentToday(f.it.uid);
        }
        role.sendNow(new FriendGiftSendResp(sentToday,sentMap));
        return null;
    }

    /**
     * 领取礼物
     * @param frienduid 好友的唯一id
     */
    public GameResult<MixRes> recvGift(String frienduid){
        GameResult<MixRes> r = new GameResult<>();
        Map<String, FriendGiftItem> targets = new HashMap<>();
        try{
            if (StringUtils.isBlank(frienduid)) {
                for(FriendGiftItem item : items.values()){
                    if(!item.recv){
                        item.recv = true;
                        targets.put(item.senderUid, item);
                    }
                }
            } else {
                FriendGiftItem item = items.get(frienduid);
                if(item != null && !item.recv){
                    item.recv = true;
                    targets.put(item.senderUid, item);
                }
            }
        }catch(Exception e){
            logger.error("领取好友礼物异常, e=", e);
        }
        if (targets.isEmpty()) {
            return r.fail("没有可以领取的礼物");
        }
        MixRes totalRecvReward = new MixRes();
        for (int i = 0; i < targets.size(); i++) {
            if (recvToday < GDFriend.GIFT_FRIENDSHIP_LIMIT) {
                totalRecvReward.addList(new MixRes(GDFriend.GIFT_RECV_FRIENDSHIP));
                recvToday += 1;
            }
        }
        if(totalRecvReward.isEmpty()){
            return r.fail("今日收礼次数已达上限，请明日再试");
        }
        role.sendNow(new FriendGiftHandleResp(targets));
        totalRecvReward.add(role, true, 0);
        role.getHistory().action.friendGiftRecvTimes(targets.size());
        role.getTrace().receiveFriendGift(targets.size());
        r.data = totalRecvReward;
        return r;
    }
}
